Skip to content

feat: handle theme with localStorage#456

Merged
schiller-manuel merged 7 commits intoTanStack:mainfrom
Balastrong:feat/theme-localstorage
Aug 7, 2025
Merged

feat: handle theme with localStorage#456
schiller-manuel merged 7 commits intoTanStack:mainfrom
Balastrong:feat/theme-localstorage

Conversation

@Balastrong
Copy link
Member

As discussed on Discord I removed the cookie logic in favor of localStorage to handle the theme (light, dark and auto)

It previously relied on zusatand, I replaced it with a react context in a separate commit, you can see both versions in this PR.
I think we should keep it simple and stay with the context but if we want to use zustand as it's already installed I can revert the second commit.

@netlify
Copy link

netlify bot commented Aug 6, 2025

Deploy Preview for tanstack ready!

Name Link
🔨 Latest commit cef7935
🔍 Latest deploy log https://app.netlify.com/projects/tanstack/deploys/689460fcb762400008d60684
😎 Deploy Preview https://deploy-preview-456--tanstack.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 30 (🔴 down 2 from production)
Accessibility: 98 (no change from production)
Best Practices: 83 (no change from production)
SEO: 95 (no change from production)
PWA: 60 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

type ThemeMode = z.infer<typeof themeModeSchema>
type ResolvedTheme = z.infer<typeof resolvedThemeSchema>

function getStoredThemeMode(): ThemeMode {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could make this an isomorphic function using createIsomorphicFn

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(this applies to the other functions as well)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the advantage?

const storedTheme = localStorage.getItem('theme') || 'auto'

if (storedTheme === 'auto') {
const autoTheme = window.matchMedia('(prefers-color-scheme: dark)')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have a subscription to theme change on the client? e.g. if i switch from dark to light mode on the client in my browser or OS settings, does this update the theme here as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's the setupPreferredListener function setting it up. While on auto if you change the theme for example on your OS setting it immediately updates on tanstack.com

document.documentElement.classList.add(storedTheme)
}
} catch (e) {
const autoTheme = window.matchMedia('(prefers-color-scheme: dark)')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we call this again? when does the above throw?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just in case accessing localStorage fails

export function ThemeProvider({ children }: ThemeProviderProps) {
const [themeMode, setThemeMode] = useState<ThemeMode>(getStoredThemeMode)

useEffect(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need that effect? cant we just execute it whenever setTheme is called?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can move updateThemeClass inside setTheme but I'd keep the useEffect to un/subscribe to the system listener

@Balastrong Balastrong force-pushed the feat/theme-localstorage branch from cef7935 to e6ffe3c Compare August 7, 2025 08:20
@schiller-manuel schiller-manuel merged commit d97b8d1 into TanStack:main Aug 7, 2025
6 checks passed
KyleAMathews pushed a commit to KyleAMathews/tanstack.com that referenced this pull request Nov 12, 2025
LeCarbonator pushed a commit to LeCarbonator/tanstack.com that referenced this pull request Jan 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants